import java.util.ArrayList;
import java.sql.*;

public class Liste {

  // Attributs
  private String nom;
  private Date dateButoire;
  private int listID;
  private static final String tableListe = "ap4_sp_liste";

  // Constructeurs
  public Liste(String nom, Date dateButoire) throws SQLException {
    this.nom = nom;
    this.dateButoire = dateButoire;

    try {
      this.createList();
    } catch (SQLException e) {
      throw e;
    }
  }

  public Liste(int listID, String nom, Date dateButoire) {
    this.nom = nom;
    this.dateButoire = dateButoire;
    this.listID = listID;
  }

  // Statics
  // Charge toutes les listes enregistrées dans la base de donnée
  public static ArrayList<Liste> chargerListes() throws SQLException {
    ArrayList listes = new ArrayList<Liste>();

    try {
      bdd db = bdd.getInstance();
      ResultSet res = db.req("SELECT * FROM " + tableListe + " ORDER BY idListe DESC");
      while (res.next()) {
        listes.add(new Liste(res.getInt("idListe"), res.getString("nom"), new Date(res.getString("dateButoire"))));
      }
      return listes;
    } catch (SQLException e) {
      throw e;
    }
  }

  // Getters & Setters
  public Date getDateButoire() {
    return dateButoire;
  }

  public int getListID() {
    return listID;
  }

  public String getNom() {
    return nom;
  }

  public void setNom(String nom) throws SQLException {
    try {
      bdd db = bdd.getInstance();
      db.update("UPDATE " + tableListe + " SET nom='" + nom + "' WHERE idListe=" + this.listID);
    } catch (SQLException e) {
      throw e;
    }
  }

  public void setDateButoire(Date dateBut) throws SQLException {
    try {
      bdd db = bdd.getInstance();
      db.update("UPDATE " + tableListe + " SET dateButoire='" + dateBut + "' WHERE idListe=" + this.listID);
    } catch (SQLException e) {
      throw e;
    }
  }

  // Methodes
  // Créé une nouvelle liste avec les paramètres nom et dateButoire
  private void createList() throws SQLException {
    try {
      bdd db = bdd.getInstance();
      db.update("INSERT INTO " + tableListe + " (nom, dateButoire) VALUES ('" + this.nom + "','" + this.dateButoire + "')");
      this.listID = this.getLastId();
    } catch (SQLException e) {
      throw e;
    }
  }

  private int getLastId() throws SQLException {
    try {
      bdd db = bdd.getInstance();
      ResultSet res = db.req("SELECT idListe FROM " + tableListe + " ORDER BY idListe DESC LIMIT 0,1");
      res.first();
      return res.getInt(1) + 1;
    } catch (SQLException e) {
      throw e;
    }
  }

  public String toString() {
    return "Liste{" + "nom=" + nom + ", dateButoire=" + dateButoire + ", listID=" + listID + '}';
  }

  public void delete() throws SQLException {
    try {
      bdd db = bdd.getInstance();
      db.update("DELETE FROM  " + tableListe + " WHERE idListe=" + this.listID);
    } catch (SQLException e) {
      throw e;
    }
  }
}
